﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SmartMathLibrary
{
    /// <summary>
    /// This class represents a complex Taylor series.
    /// </summary>
    [Serializable]
    public class TaylorComplexSeries
    {
        /// <summary>
        /// The constant value of a Taylor series
        /// </summary>
        private ComplexNumber constantValue;

        /// <summary>
        /// The other components of the specified Taylor series.
        /// </summary>
        private readonly List<TaylorComplexSeriesComponent> components;

        /// <summary>
        /// Initializes a new instance of the <see cref="TaylorComplexSeries"/> class.
        /// </summary>
        public TaylorComplexSeries()
        {
            this.components = new List<TaylorComplexSeriesComponent>();
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="TaylorComplexSeries"/> class.
        /// </summary>
        /// <param name="components">The components of the specified Taylor series.</param>
        /// <param name="constantValue">The constant value of the specified Taylor series.</param>
        public TaylorComplexSeries(List<TaylorComplexSeriesComponent> components, ComplexNumber constantValue)
        {
            if (components == (List<TaylorComplexSeriesComponent>) null)
            {
                throw new ArgumentNullException("components");
            }

            this.constantValue = constantValue;
            this.components = components;
        }

        /// <summary>
        /// Gets or sets the components of the Taylor series.
        /// </summary>
        /// <value>The components of the Taylor series.</value>
        public List<TaylorComplexSeriesComponent> Components
        {
            get { return components; }
        }

        /// <summary>
        /// Gets or sets the constant value of the Taylor series.
        /// </summary>
        /// <value>The constant value of the Taylor series.</value>
        public ComplexNumber ConstantValue
        {
            get { return constantValue; }
            set { constantValue = value; }
        }

        /// <summary>
        /// Solves the Taylor series at the specified position x.
        /// </summary>
        /// <param name="x">The specified position x.</param>
        /// <returns>The value p(x) for the specified x value.</returns>
        public ComplexNumber SolveAt(ComplexNumber x)
        {
            ComplexNumber result = this.constantValue.Copy();

            foreach (TaylorComplexSeriesComponent component in this.components)
            {
                result += component.SolveAt(x);
            }

            return result;
        }
    }
}